﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web;
using Timesheet.Domain;
using Timesheet.Service;

namespace Timesheet.WebForm.Projects
{
    public partial class ProjectMrg:System.Web.UI.Page
    {
        public IProjectService _ProjectService { get; set; }
        public IPrjReferencesService _PrjReferencesService { get; set; }
        public IUserService _UserService { get; set; }
        public ILocationsService _LocationsService { get; set; }
        public IDisciplinesService _DisciplinesService { get; set; }
        public ITeamService _TeamService { get; set; }
        public ITeamDetailService _TeamDetailService { get; set; }

        public IList<User> users = new List<User>();
        protected void Page_Load(object sender,EventArgs e)
        {
            if(!IsPostBack)
            {
                var user = (User)Session["User"];
                if (user == null)
                    Response.Redirect("~/Account/Login.aspx");
                long projectId= Convert.ToInt64(Request.Params["id"]);
                long userId=Convert.ToInt64(Session["UserId"]);
                bool value = _ProjectService.checkOwnerProject(userId, projectId);
               
                if (value == false || !user.Manager )
                {
                    Response.Redirect("~/Projects/ProjectMrgCoord.aspx?id=" + projectId);
                    return;
                }

                var prj = _ProjectService.GetByID(Convert.ToInt64(Request.Params["id"]));
                if(prj != null)
                {
                    txtProjectName.Value = prj.ProjectName;
                    txtCustomer.Value = prj.CustomerInfo;
                    txtJob.Value = prj.JobInfo;
                    txtJobDescription.Value = prj.JobDes;
                    txtContact.Value = prj.Contact;
                    txtNetwork.Value = prj.NetCode;
                    txtResp.Value = prj.ResponsePerson;
                    txtTicket.Value = prj.Ticket;
                    txtWbs.Value = prj.Wbs; 
                    chbActive.Value = prj.Active;
                }
                users = _UserService.GetCoodinators();
            }
            grvLocations.FocusedRowIndex = 0;
        }
         protected void dsLocations_ObjectCreating(object sender,ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = _LocationsService;
        }
        protected void dsRef_ObjectCreating(object sender,ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = _PrjReferencesService;
        }

        protected void dsCoords_ObjectCreating(object sender,ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = _UserService;
        }

        protected void btSave_Click(object sender,EventArgs e)
        {
             var prj = _ProjectService.GetByID(Convert.ToInt64(Request.Params["id"]));

            prj.ProjectName = txtProjectName.Value+"";
            prj.CustomerInfo = txtCustomer.Value+"";
            prj.JobInfo = txtJob.Value+"";
            prj.JobDes = txtJobDescription.Value+"";
            prj.Contact = txtContact.Value+"";
            prj.NetCode = txtNetwork.Value+"";
            prj.ResponsePerson = txtResp.Value+"";
            prj.Ticket = Convert.ToInt32(txtTicket.Value);
            prj.Active = chbActive.Checked;
            prj.Wbs = txtWbs.Value + "";
            _ProjectService.Update(prj);

        }

        protected void btnNewLocation_Click(object sender,EventArgs e)
        {
            grvLocations.AddNewRow();
        }

        protected void btnNewCoodinator_Click(object sender,EventArgs e)
        {
            grvCoordinator.AddNewRow();
        }

        protected void grvCoordinator_RowInserting(object sender,DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
            e.NewValues["ProjID"] = Convert.ToInt64(Request.Params["id"]);
            e.NewValues["Type"] = "2";   
            e.NewValues["RefNames"] = "";
        }

        protected void grvCoordinator_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
        {
            e.NewValues["ProjID"] = Convert.ToInt64(Request.Params["id"]);
            e.NewValues["Type"] = "2";
            e.NewValues["RefNames"] = "";
           
        }

        protected void grvLocations_RowInserting(object sender,DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
            e.NewValues["ProjID"] = Convert.ToInt64(Request.Params["id"]);
            e.NewValues["Type"] = "1";            
        }

        protected void grvLocations_InitNewRow(object sender,DevExpress.Web.Data.ASPxDataInitNewRowEventArgs e)
        {
          
        }

        protected void grid_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e)
        {
            if (e.Column.FieldName == "RefID")
            {
                ASPxComboBox cmb = e.Editor as ASPxComboBox;
                cmb.DataSource = _UserService.GetCoodinatorsNotInProjectId(Convert.ToInt64(Request.Params["id"]));
                cmb.ValueField = "Id";
                cmb.ValueType = typeof(Int64);
                cmb.TextField = "FullName";
                cmb.DataBindItems();
            }
        }

        protected void grvCoordinator_CustomColumnDisplayText(object sender, DevExpress.Web.ASPxGridViewColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName != "RefID") return;
            if (Convert.ToInt64(e.Value) == 0)
                e.DisplayText = "empty";
            else
            {
                User user = users.FirstOrDefault(x => x.Id == Convert.ToInt64(e.Value));
                if(user != null)
                 e.DisplayText = user.FullName;
                else
                    e.DisplayText = "empty";
            }
        }



        //Team 
        protected void dsRefLocations_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = _PrjReferencesService;
        }

        protected void dsDisciplines_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = _DisciplinesService;
        }

        protected void dsTeams_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = _TeamService;
        }

        protected void dsEmployers_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = _UserService;
        }

        protected void dsTeamDetails_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = _TeamDetailService;
        }

        protected void grvTeamDetails_BeforePerformDataSelect(object sender, EventArgs e)
        {
            Session["teamID"] = (sender as ASPxGridView).GetMasterRowKeyValue();
        }

        protected void btnNewLead_Click(object sender, EventArgs e)
        {
            //var masterKey = grvTeams.GetMasterRowKeyValue();
            //ASPxGridView detailG = grvTeams.FindRowTemplateControlByKey(masterKey,"grvTeamDetails") as ASPxGridView;
            //detailG.AddNewRow();
        }

        protected void btnNewTeam_Click(object sender, EventArgs e)
        {
            grvTeams.AddNewRow();
        }

        protected void grvTeams_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
            e.NewValues["ProjectID"] = Convert.ToInt64(Request.Params["id"]);
        }

        protected void grvTeamDetails_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
            e.NewValues["TeamID"] = Convert.ToInt64(Session["teamID"]);
        }


        protected void grvTeamDetails_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
        {
            e.NewValues["TeamID"] = Convert.ToInt64(Session["teamID"]);
        }

        protected void grvTeams_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
        {
            e.NewValues["ProjectID"] = Convert.ToInt64(Request.Params["id"]);
        }

    }
}